#!/usr/bin/env python

__version__ = "0.1"
__author__  = "Glenn Hope"
__email__   = "hopega@email.wofford.edu"

from Milk import Milk
from optparse import Option, OptionParser

#TODO: change this to use ConfigParser
import milkConfig

class MyOption(Option):
    """Add an "extend" option to allow for comma-delimited parameters."""
    ACTIONS = Option.ACTIONS + ("extend",)
    STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
    TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
    ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)

    def take_action(self, action, dest, opt, value, values, parser):
        if action == "extend":
            lvalue = value.split(",")
            values.ensure_value(dest, []).extend(lvalue)
        else:
            Option.take_action(
                self, action, dest, opt, value, values, parser)

def parseOpts():
    parser = OptionParser(option_class=MyOption,
                          usage="%prog [OPTION]... [TASKNAME]...",
                          version="%prog " + __version__)

    parser.add_option("-q", "--quiet",
            action="store_false",
            dest="verbose",
            default=True,
            help="don't print status messages to stdout")

    parser.add_option("-l", "--list", 
            dest="listname", 
            default=milkConfig.defaultList,
            help="specify a list", 
            metavar="list")

    parser.add_option("-p", "--print",
            action="store_true",
            dest="printlist",
            default=False,
            help="print the specified list")

    parser.add_option("-t", "--tags", 
            dest="tags", 
            help="tags for a task (may be comma-delimited)", 
            action="extend",
            metavar="tags")

    parser.add_option("--no-smart-add",
            action="store_false",
            dest="smartadd",
            default=True,
            help="don't use Smart Add when adding tasks")

    return parser.parse_args()

if __name__ == "__main__":
    (options, args) = parseOpts()
    milk = Milk()

    list_id = milk.getListID(options.listname)
    smartadd = options.smartadd
    printlist = options.printlist

    if len(args) == 0:
        milk.printFormattedTaskInfo(list_id)
    else:
        for task in args:
            milk.addTask(task, list_id, smartadd)
